package com.shoeshopping.service.zx;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shoeshopping.dao.BaseDao;
import com.shoeshopping.entity.Order;
import com.shoeshopping.entity.OrderItem;
import com.shoeshopping.formbean.zx.OrderAdminForm;
import com.shoeshopping.utils.zx.EasyUIResult;
import com.shoeshopping.vo.zx.VoOrderAdmin;
import com.shoeshopping.vo.zx.VoOrderItemAdmin;

@Service("orderAdminService")
@Transactional
public class OrderAdminServiceImpl implements OrderAdminService{

	@Resource
	BaseDao baseDao;
	
	@Override
	public EasyUIResult list(OrderAdminForm dataForm) {
		String hql = "from Order as o";
		List allList = baseDao.find(hql,null);
		
		List list = baseDao.findWithPageAndSort(hql, null, 
				dataForm.getPage(), dataForm.getRows(),
				dataForm.getSort(), dataForm.getOrder());
		
		List voList = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			Order order = (Order) list.get(i);
			VoOrderAdmin orderAdmin = new VoOrderAdmin(order);
			
			voList.add(orderAdmin);
		}
		
		return new EasyUIResult((long) allList.size(), voList);
	}

	@Override
	public VoOrderAdmin edit(OrderAdminForm dataForm) {
		String hql = "from Order as o where o.id=?";
		List parma = new ArrayList();
		parma.add(dataForm.getId());
		Order order = (Order) baseDao.find(hql, parma).get(0);
		VoOrderAdmin voOrderAdmin = new VoOrderAdmin(order); 
		return voOrderAdmin;
	}

	@Override
	public boolean add(OrderAdminForm dataForm) {
		
		if(dataForm.getId() != null && dataForm.getId().length() > 0){
			String hql = "from Order as o where o.id=?";
			List parma = new ArrayList();
			parma.add(dataForm.getId());
			List list = baseDao.find(hql, parma);
			if(list.size() > 0){
				Order listOrder = (Order) list.get(0);
				listOrder.getUser().setUsername(dataForm.getUsername());
				listOrder.getDeliveryAddress().setAddress(dataForm.getDeliveryAddress());
				listOrder.getDeliveryAddress().setPhone(dataForm.getDeliveryPhone());
				listOrder.getDeliveryAddress().setName(dataForm.getDeliveryName());
				baseDao.update(listOrder);
				return true;
			}
		} 
		return false;
	}

	@Override
	public void delete(OrderAdminForm dataForm) {
		Order order = new Order();
		order.setId(dataForm.getId());
		baseDao.delete(order);
	}

	@Override
	public EasyUIResult listItem(OrderAdminForm dataForm) {
		if(dataForm.getId()  != null && dataForm.getId().length() > 0){
			String hql = "from Order as o where id=?";
			
			List parma = new ArrayList();
			parma.add(dataForm.getId());
			//System.out.println("dataForm = "+dataForm.getId());
			Order order = (Order) baseDao.find(hql,parma).get(0);
			
			List list = order.getOrderItem();
			
			List voList = new ArrayList();
			for (int i = 0; i < list.size(); i++) {
				OrderItem orderItem = (OrderItem) list.get(i);
				VoOrderItemAdmin orderItemAdmin = new VoOrderItemAdmin(orderItem);
				
				voList.add(orderItemAdmin);
			}
			return new EasyUIResult((long) 0, voList);
		}
		return null;
	}

}
